var vm = new Vue({
    el:"#app",
    data(){
        return {
            //初始化
            UserNo:'',
            UserName:'',
            CarNoList:[],
            //搜索条件
            SjName:'',
            BrFlag:'',
            CarNo:'',
            BreakRuleStartDate:'',
            BreakRuleEndDate:'',

            SjName:'',
            SjModel: false, //司机弹框
            columns2: [
                {
                    type: 'index',
                    title: '序号',
                    width:46,
                    align: 'center',
                },
                {
                    title: '员工编号',
                    key: 'UserNo',
                    width:90,
                    align: 'center',
                },
                {
                    title: '中心店面',
                    key: 'Store',
                    width:75,
                    align: 'center',
                },
                {
                    title: '部门',
                    key: 'Bm',
                    width:75,
                    align: 'center',
                },
                {
                    title: '姓名',
                    key: 'Name',
                    width:75,
                    align: 'center',
                },
                {
                    title: '性别',
                    key: 'Sex',
                    width:100,
                    align: 'center',
                    render:(h,params)=> {
                        var Sex = params.row.Sex;
                        if(Sex=="0"){
                            Sex = '男';
                        }
                        if(Sex=="1"){
                            Sex = '女';
                        }
                        return h('span',Sex);
                    }
                },
                {
                    title: '生日',
                    key: 'Birthday',
                    width:100,
                    align: 'center',
                },
                {
                    title: '手机号',
                    key: 'PhoneNo',
                    width:100,
                    align: 'center',
                },
                {
                    title: '准驾车型',
                    key: 'CanUseCarType',
                    width:100,
                    align: 'center',
                },
                {
                    title: '出车状态',
                    key: 'Status',
                    width:75,
                    align: 'center',
                    render:(h,params)=> {
                        var Status = params.row.Status;
                        if(Status=="0"){
                            Status = '空闲';
                        }
                        if(Status=="1"){
                            Status = '出车中';
                        }
                        return h('span',Status);
                    }
                },
                {
                    title: '初次领证日期',
                    key: 'StartHaveLicenseDate',
                    width:90,
                    align: 'center',
                },
                {
                    title: '驾驶证有效期至',
                    key: 'LicenseUntil',
                    width:100,
                    align: 'center',
                },
            ],
            data2:[],


            modal1: false, //添加违章记录弹框
            formItem: {
                CreateManNo: '',
                CreateManName: '',
                CarNo: '',
                SjNo: '',
                SjName: '',
                BreakRuleInfo: '',
                BreakRulePlace: '',
                Score: '',
                Fine: '',
                BreakRuleDate:'',
                BreakRuleTime: '',
                DealDate: '',
                DealTime: '',
                CreateTime: '',
                Remark: '',
            },
            editmodal1:false,//编辑加油记录弹框
            editformItem: {
                ID:'',
                CreateManNo: '',
                CreateManName: '',
                CarNo: '',
                SjNo:'',
                SjName:'',
                DealDate:'',
                DealTime:'',
                BreakRuleInfo:''
            },
            //搜索结果
            columns1: [
                {
                    type: 'index',
                    title: '序号',
                    width:46,
                    align: 'center',
                },
                {
                    title: '违章车辆',
                    key: 'CarNo',
                    align: 'center',
                },
                {
                    title: '驾驶人',
                    key: 'SjName',
                    align: 'center',
                    width:95
                },
                {
                    title: '违章时间',
                    key: 'BreakRuleTime',
                    align: 'center',
                    width:130
                },
                {
                    title: '记分',
                    key: 'Score',
                    align: 'center',
                },
                {
                    title: '罚款（元）',
                    key: 'Fine',
                    align: 'center',
                },
                {
                    title: '违章地点',
                    key: 'BreakRulePlace',
                    align: 'center',
                },
                {
                    title: '违章信息',
                    key: 'BreakRuleInfo',
                    align: 'center',
                },
                {
                    title: '是否处理',
                    key: 'DealFlag',
                    align: 'center',
                    render:(h,params)=> {
                        var DealFlag = params.row.DealFlag;
                        if(DealFlag=="1"){
                            DealFlag = '已处理';
                        }
                        if(DealFlag=="0"){
                            DealFlag = '未处理';
                        }
                        return h('span',DealFlag);
                    }
                },
                {
                    title: '处理时间',
                    key: 'DealTime',
                    align: 'center',
                },
                {
                    title: '处理人',
                    key: 'DealManName',
                    align: 'center',
                },
                {
                    title: '备注',
                    key: 'Remark',
                    align: 'center',
                },
                {
                    title: '编辑人',
                    key: 'CreateManName',
                    align: 'center',
                },
                {   title: '操作',
                    key: 'action',
                    align: 'center',
                    width:150,
                    render: (h, params) => {
                        return h('div', [
                            h('a', {
                                style: {
                                    marginRight: '5px',
                                    border : '0',
                                    fontSize: '14px',
                                },
                                on: {
                                    click: () => {
                                        var self = this;
                                        self.editformItem.ID = params.row.ID;
                                        self.editformItem.CreateManNo = self.UserNo;
                                        self.editformItem.CreateManName = self.UserName;
                                        self.editformItem.CarNo = params.row.CarNo;
                                        self.editformItem.SjNo = params.row.SjNo;
                                        self.editformItem.SjName = params.row.SjName;
                                        self.editformItem.BreakRuleInfo = params.row.BreakRuleInfo;
                                        this.openeditmodel();
                                    }
                                }
                            },'处理'),
                            h('span', {
                            },'|'),
                            h('a', {
                                style: {
                                    marginLeft: '5px',
                                    border : '0',
                                    fontSize: '14px',
                                },
                                on: {
                                    click: () => {
                                        var self = this;
                                        var ID = params.row.ID;
                                        var SjNo = params.row.SjNo;
                                        self.confirm(ID,SjNo);
                                    }
                                }
                            },'删除')
                        ])
                    }
                }
            ],
            data1: [],
        }
    },
    created(){
        this.initThisPage();
        this.queryAllCarNo();
    },
    methods:{
        ok () {
        },
        cancel () {
        },
        initThisPage(){
            var self = this;
            $.ajax({
                url: "./goPCDD/initPage", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                type: "GET", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.status==0){
                        // top.location.href = "../../login.html";
                        window.location.href="../../login.html";
                    }else{
                        self.UserNo = data.userno;
                        self.UserName = data.username;
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function() {}
            });
        },
        queryAllCarNo(){
            var self = this;
            $.ajax({
                url: "./goPCDD/queryAllCarNo", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                type: "GET", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    var carnojson = new Array();
                    var json = JSON.parse(data.data);
                    var tempcarnojson = json.data;

                    for(var i=0;i<tempcarnojson.length;i++){
                        var temp = {
                            value: tempcarnojson[i].CarNo,
                            label: tempcarnojson[i].CarNo
                        };
                        carnojson.push(temp)
                    }
                    self.CarNoList = carnojson;
                },
                error: function() {},
            });
        },
        goSjModel(){
            this.querysjtable('1','999999999999');
            this.SjModel = true;
        },
        //查询所有司机
        querysjtable(pageNum,pageSize){
            var info ;
            var total;
            var Name = this.SjName;
            var jsondata={"page":pageNum, "row":pageSize,"Name":Name};
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goPCDD/queryAllMen", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                data: data_json, //参数值
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    info = eval(data.data);
                    total = data.totalnum;
                },
                error: function() {},
            });
            if(info){
                this.data2 = info;
                this.dataCount2 = total;
            }
        },
        sjRowClic(CurrentRow){
            this.formItem.SjNo = CurrentRow.UserNo;
            this.formItem.SjName = CurrentRow.Name;
            this.SjModel = false;
        },
        //打开添加加油记录弹框
        openModel1(){
            this.formItem.CreateManName = this.UserName;
            this.formItem.CreateManNo = this.UserNo;
            this.modal1 = true;
        },
        //打开编辑加油记录弹框
        openeditmodel(){
            var self = this;
            self.editmodal1 = true;
        },
        //保存违章记录
        saveBreakRule(){
            var self = this;
            var CreateManNo = self.formItem.CreateManNo;
            var CreateManName = self.formItem.CreateManName;
            var CarNo = self.formItem.CarNo;
            if(CarNo==null||CarNo==""){
                toastr.warning("请选择车牌号");
                return false;
            }
            var SjName = self.formItem.SjName;
            var SjNo = self.formItem.SjNo;
            if(SjName==null||SjName==""||SjNo==null||SjNo==""){
                toastr.warning("请选择驾驶人");
                return false;
            }

            var BreakRuleTimeStr = '';
            var BreakRuleDate = self.formItem.BreakRuleDate;
            if(BreakRuleDate==null||BreakRuleDate==""){
                toastr.warning("请选择违章日期年月日");
                return false;
            }
            var tempBreakRuleDate = new Date(BreakRuleDate);
            BreakRuleDate = tempBreakRuleDate.toLocaleDateString();
            var BreakRuleTime = self.formItem.BreakRuleTime;
            if(BreakRuleTime==null||BreakRuleTime==""){
                BreakRuleTimeStr = BreakRuleDate;
            }else{
                BreakRuleTimeStr = BreakRuleDate +" "+BreakRuleTime;
            }

            var Score = self.formItem.Score;
            if(Score==null||Score==""){
                toastr.warning("请录入记分");
                return false;
            }else{
                if(self.isPositiveInteger(Score)){}else{
                    toastr.warning("记分请录入正整数");
                    return false;
                }
            }
            var Fine = self.formItem.Fine;
            if(Fine==null||Fine==""){
                toastr.warning("请录入罚款金额");
                return false;
            }else{
                if(self.isPositiveInteger(Fine)){}else{
                    toastr.warning("罚款金额请录入正整数");
                    return false;
                }
            }
            var BreakRulePlace = self.formItem.BreakRulePlace;
            if(BreakRulePlace==null||BreakRulePlace==""){
                toastr.warning("请录入违章地点");
                return false;
            }

            var BreakRuleInfo = self.formItem.BreakRuleInfo;
            if(BreakRuleInfo==null||BreakRuleInfo==""){
                toastr.warning("请录入违章信息");
                return false;
            }

            /*var DealTimeStr = '';
            var DealDate = self.formItem.DealDate;
            if(DealDate==null||DealDate==""){
                DealDate = null;
            }else{
                var tempDealDate = new Date(DealDate);
                DealDate = tempDealDate.toLocaleDateString();
                var DealTime = self.formItem.DealTime;
                if(DealTime==null||DealTime==""){
                    DealTimeStr = DealDate;
                }else{
                    DealTimeStr = DealDate +" "+DealTime;
                }
            }*/

            var Remark = self.formItem.Remark;

            var jsondata = {
                "CreateManNo":CreateManNo,
                "CreateManName":CreateManName,
                "CarNo":CarNo,
                "SjName":SjName,
                "SjNo":SjNo,
                "BreakRuleTime":BreakRuleTimeStr,
                "Score":Score,
                "Fine":Fine,
                "BreakRulePlace":BreakRulePlace,
                "BreakRuleInfo":BreakRuleInfo,
                "Remark":Remark,
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goPCDD/saveBreakRule", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.status=='1'){
                        toastr.success('保存成功');
                        self.formItem.CreateManNo = "";
                        self.formItem.CreateManName = "";
                        self.formItem.CarNo = "";

                        self.formItem.SjNo = "";
                        self.formItem.SjName = "";
                        self.formItem.BreakRuleInfo = "";
                        self.formItem.BreakRulePlace = "";
                        self.formItem.Score = "";
                        self.formItem.Fine = "";
                        self.formItem.BreakRuleDate = "";
                        self.formItem.BreakRuleTime = "";
                        self.formItem.DealDate = "";
                        self.formItem.DealTime = "";
                        self.formItem.CreateTime = "";
                        self.formItem.Remark = "";
                        self.querytable();
                    }else if(data.status=='-1'){
                        toastr.warning('保存司机违规状态失败');
                    }else{
                        toastr.error("保存失败，请重新保存");
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        //处理违章记录
        editBreakRule(){
            var self = this;
            var ID = self.editformItem.ID;
            var DealManNo = self.editformItem.CreateManNo;
            var DealManName = self.editformItem.CreateManName;
            var SjNo = self.editformItem.SjNo;
            var DealDate = self.editformItem.DealDate;
            if(DealDate==null||DealDate==""){
                toastr.warning("请选择处理日期年月日");
                return false;
            }
            var tempDealDate = new Date(DealDate);
            DealDate = tempDealDate.toLocaleDateString();

            var DealTime = self.editformItem.DealTime;
            if(DealTime==null||DealTime==""){
                toastr.warning("请选择处理日期时分秒");
                return false;
            }
            var DealTimeStr = DealDate +" "+DealTime;

            var jsondata = {
                "ID":ID,
                "DealManNo":DealManNo,
                "DealManName":DealManName,
                "SjNo":SjNo,
                "DealTime":DealTimeStr,
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goPCDD/editBreakRule", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.status=='1'){
                        toastr.success('编辑成功');
                        self.editformItem.CreateManNo = "";
                        self.editformItem.CreateManName = "";
                        self.editformItem.CarNo = "";

                        self.editformItem.SjNo = "";
                        self.editformItem.SjName = "";
                        self.editformItem.DealDate = "";
                        self.editformItem.DealTime = "";
                        self.editformItem.BreakRuleInfo = "";
                        self.querytable();
                    }else if(data.status=='2'){
                        toastr.warning('编辑成功,但当前司机还有违章信息未处理');
                        self.editformItem.CreateManNo = "";
                        self.editformItem.CreateManName = "";
                        self.editformItem.CarNo = "";

                        self.editformItem.SjNo = "";
                        self.editformItem.SjName = "";
                        self.editformItem.DealDate = "";
                        self.editformItem.DealTime = "";
                        self.editformItem.BreakRuleInfo = "";
                        self.querytable();
                    }else{
                        toastr.error('编辑失败，请重新录入');
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        querytable(){
            var self = this;
            var info ;
            var BrFlag = self.BrFlag;
            var CarNo = self.CarNo;
            var SjName = self.SjName;
            var BreakRuleStartDate = self.BreakRuleStartDate;
            if(BreakRuleStartDate==''||BreakRuleStartDate==null){
                BreakRuleStartDate = '';
            }else{
                var tempBreakRuleStartDate = new Date(BreakRuleStartDate);
                BreakRuleStartDate = tempBreakRuleStartDate.toLocaleDateString();
            }


            var BreakRuleEndDate = self.BreakRuleEndDate;
            if(BreakRuleEndDate==''||BreakRuleEndDate==null){
                BreakRuleEndDate = '';
            }else{
                var tempBreakRuleEndDate = new Date(BreakRuleEndDate);
                BreakRuleEndDate = tempBreakRuleEndDate.toLocaleDateString();
            }

            var jsondata={"BrFlag":BrFlag,"CarNo":CarNo,"BreakRuleStartDate":BreakRuleStartDate,"BreakRuleEndDate":BreakRuleEndDate,"SjName":SjName};
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goPCDD/queryAllBreakRule", //请求的url地址
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                async:false,
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    info = data.data;
                },
                error: function() {},
            });
            if(info){
                self.data1 = info;
            }
        },
        confirm (ID,SjNo) {
            var self = this;
            this.$Modal.confirm({
                title: '提示',
                content: '<p>是否删除</p>',
                onOk: () => {
                    self.delBreakRule(ID,SjNo);
                },
                onCancel: () => {
                    toastr.info("取消删除");
                }
            });
        },
        // 删除弹框
        delBreakRule(ID,SjNo){
            var self = this;
            var jsondata = {
                "ID":ID,
                "SjNo":SjNo
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goPCDD/delBreakRule", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.status=='1'){
                        toastr.success("删除成功");
                        self.querytable();
                    }else if(data.status=='2'){
                        toastr.warning("删除成功,但当前司机还有违章记录");
                        self.querytable();
                    }else{
                        toastr.error('删除失败，请刷新页面后重试');
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        //手机号验证
        isPoneAvailable(str) {
            var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
            if (!myreg.test(str)) {
                return false;
            } else {
                return true;
            }
        },
        //uuid
        uuid(len, radix) {
            var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
            var uuid = [], i;
            radix = radix || chars.length;

            if (len) {
                // Compact form
                for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
            } else {
                // rfc4122, version 4 form
                var r;

                // rfc4122 requires these characters
                uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
                uuid[14] = '4';

                // Fill in random data. At i==19 set the high bits of clock sequence as
                // per rfc4122, sec. 4.1.5
                for (i = 0; i < 36; i++) {
                    if (!uuid[i]) {
                        r = 0 | Math.random()*16;
                        uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
                    }
                }
            }

            return uuid.join('');
        },
        //两位小数验证
        isMianJi(mj){
            var reg = /^[1-9]+\d*(\.\d{0,2})?$|^0?\.\d{0,2}$/;
            if(reg.test(mj)){
                return true;
            }else{
                return false;
            }
        },
        //正整数验证
        isPositiveInteger(s){//是否为正整数
            var re = /^[0-9]+$/ ;
            return re.test(s)
        }
    }
});